package com.societyGames.flashForms
{
	import com.societyGames.system.EventObject;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;

	public class Splitter extends Sprite
	{
		private var _sprite:Sprite;
		private var _bounds:Rectangle;
		private var _cursor:DisplayObject;
		private var _draggedEventObject:EventObject;
		private var _droppedEventObject:EventObject;
		private var _desiredWidth:Number;
		private var _desiredHeight:Number;

		public function Splitter(sprite:Sprite, cursor:DisplayObject)
		{
			this._draggedEventObject = new EventObject(this, "DRAGGED");
			this._droppedEventObject = new EventObject(this, "DROPPED");
			this._sprite = sprite;
			this._sprite.addEventListener(MouseEvent.ROLL_OVER, splitterRollOverHandler);
			this._sprite.addEventListener(MouseEvent.ROLL_OUT, splitterRollOutHandler);
			this._sprite.addEventListener(MouseEvent.MOUSE_DOWN, splitterDownHandler);
			this.addChild(this._sprite);
			this._cursor = cursor;
		}

		public function set bounds(value:Rectangle):void
		{
			this._bounds = value;
		}
		
		public function get bounds():Rectangle
		{
			return this._bounds;			
		}
		
		public override function set width(value:Number):void
		{	
			this._desiredWidth = value;
			this._sprite.width = value;
		}
		
		public override function set height(value:Number):void
		{	
			this._desiredHeight = value;
			this._sprite.height = value;
		}
		
		public function get draggedEventObject():EventObject
		{
			return this._draggedEventObject;
		}
		
		public function get droppedEventObject():EventObject
		{
			return this._droppedEventObject;
		}
		
		private function splitterRollOverHandler(event:MouseEvent):void
		{
			MouseCursor.instance.lockCursor(this._cursor, this, -this._cursor.width / 2, -this._cursor.height / 2);
		}
		
		private function splitterRollOutHandler(event:MouseEvent):void
		{
			MouseCursor.instance.unlockCursor(this);
		}
		
		private function splitterDownHandler(event:MouseEvent):void
		{
			//Stop listening to normal events and enter the down state.
			this._sprite.removeEventListener(MouseEvent.ROLL_OVER, splitterRollOverHandler);
			this._sprite.removeEventListener(MouseEvent.ROLL_OUT, splitterRollOutHandler);
			this._sprite.removeEventListener(MouseEvent.MOUSE_DOWN, splitterDownHandler);
			this.stage.addEventListener(MouseEvent.MOUSE_UP, splitterUpHandler);
			this.stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
			
			//Start the actual drag.
			this.startDrag(false, this._bounds);
		}
		
		private function splitterUpHandler(event:MouseEvent):void
		{
			//Restore all the normal listeners
			this._sprite.addEventListener(MouseEvent.ROLL_OVER, splitterRollOverHandler);
			this._sprite.addEventListener(MouseEvent.ROLL_OUT, splitterRollOutHandler);
			this._sprite.addEventListener(MouseEvent.MOUSE_DOWN, splitterDownHandler);
			this.stage.removeEventListener(MouseEvent.MOUSE_UP, splitterUpHandler);
			this.stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
			
			//If the cursor somehow migrated off of the splitter, unlock it.
			if (!this._sprite.hitTestPoint(event.stageX, event.stageY, true))
			{
				MouseCursor.instance.unlockCursor(this);
			}
			
			//Stop the drag.
			this.stopDrag();
			
			//Raise event
			this._droppedEventObject.dispatchEvent();
		}
		
		private function mouseMoveHandler(event:MouseEvent):void
		{
			this._draggedEventObject.dispatchEvent();
		}
	}
}